//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core;

namespace OpenLS.Spreadsheet
{
    sealed class UndoUnitScope : IDisposable
    {
        public UndoUnitScope(ICompositeUndoUnit unit, Workbook workbook, UndoManager manager)
        {

            if (manager != null && manager.IsEnabled)
            {
                this._manager = manager;
                manager.Open(unit);
                this._unit = unit;
                this._workbook = workbook;
            }
        }
        public UndoUnitScope(ICompositeUndoUnit unit)
        {

            this._unit = unit;

        }

        readonly ICompositeUndoUnit _unit;
        readonly Workbook _workbook;
        private readonly UndoManager _manager;

        #region IDisposable Members

        public void Dispose()
        {
            if (_unit != null)
            {
                if (_manager != null)
                    _manager.Close(_unit);
            }
            if (_workbook != null)
                _workbook.CurrentUndoScope = null;
        }


        #endregion
    }
}
